package com.audaque.springboot.foshanupload.webfluxredis.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.server.EnableRedisWebSession;

/**
 * @author zgb
 * @desc ...
 * @date 2023-07-30 22:32:46
 */
@EnableRedisWebSession
@Configuration
public class ReactiveRedisConfig {
/*    private ReactiveRedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(ReactiveRedisTemplate reactiveRedisTemplate) {
        this.redisTemplate = reactiveRedisTemplate;
    }*/
    @Bean
    public RedisSerializationContext redisSerializationContext() {
        RedisSerializationContext.RedisSerializationContextBuilder builder = RedisSerializationContext.newSerializationContext();
        //key的序列化方式为String,注意key我们一般不会序列化为json
        builder.key(StringRedisSerializer.UTF_8);
        //设置具体的序列化类型,这样这个template就是专门序列化某种类型数据,在声明ReactiveRedisTemplate变量的时候就可以执行具体泛型
        builder.value(RedisSerializer.json());//设置value的序列化方式
        builder.hashKey(StringRedisSerializer.UTF_8);//hashkey的序列化方式为String,注意key我们一般不会序列化为json
        builder.hashValue(StringRedisSerializer.UTF_8);//hashvalue的序列化方式为String

        return builder.build();
    }
    //重新创建template
    @Bean
    public ReactiveRedisTemplate reactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory,RedisSerializationContext redisSerializationContext) {
        ReactiveRedisTemplate reactiveRedisTemplate = new ReactiveRedisTemplate(connectionFactory,redisSerializationContext);//根据上面的配置创建template
        return reactiveRedisTemplate;
    }


}
